package com.ktjy.ktmall.controller;

import com.ktjy.common.annotation.Log;
import com.ktjy.common.core.controller.BaseController;
import com.ktjy.common.core.domain.AjaxResult;
import com.ktjy.common.core.page.TableDataInfo;
import com.ktjy.common.enums.BusinessType;
import com.ktjy.common.utils.poi.ExcelUtil;
import com.ktjy.ktmall.domain.dto.CartDTO;
import com.ktjy.ktmall.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping("/ktmall/cart")
public class CartController extends BaseController {

    @Autowired
    private CartService cartService;

    /**
     * 查询购物车列表
     */
    @PreAuthorize("@ss.hasPermi('ktmall:cart:list')")
    @GetMapping("/list")
    public TableDataInfo list(CartDTO cartDTO) {
        startPage();
        List<CartDTO> list = cartService.selectCartList(cartDTO);
        return getDataTable(list);
    }

    /**
     * 导出购物车信息
     */
    @PreAuthorize("@ss.hasPermi('ktmall:cart:export')")
    @Log(title = "购物车信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CartDTO cartDTO) {
        try {
            List<CartDTO> list = cartService.selectCartList(cartDTO);
            ExcelUtil<CartDTO> util = new ExcelUtil<>(CartDTO.class);
            util.exportExcel(response, list, "购物车信息");
        } catch (Exception e) {
            throw new RuntimeException("导出购物车信息失败");
        }
    }


    /**
     * 获取购物车详细信息
     */
    @PreAuthorize("@ss.hasPermi('ktmall:cart:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return success(cartService.getById(id));
    }

    /**
     * 删除购物车信息
     */
    @PreAuthorize("@ss.hasPermi('ktmall:cart:remove')")
    @Log(title = "购物车", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable List<Integer> ids) {
        return toAjax(cartService.removeByIds(ids));
    }
}
